﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Protocols;
using System.Web.UI;
using System.Web.UI.WebControls;
using MedicWeb.MedicService;

namespace MedicWeb.forms.Print.Edit {
    public partial class InvoiceEdit : SessionPage {
        private PrintTemplateInvoice printTemplateInvoice;
        protected void Page_Load(object sender, EventArgs e) {
            string s = Request.QueryString["id"];
            int id = s == null ? 0 : int.Parse(s);
            Form.Action = ResolveUrl("~/forms/Print/Edit/InvoiceEdit.aspx");
            var ms = new MainService();
            if (id > 0) {
                Form.Action += "?id=" + id;
                printTemplateInvoice = ms.LoadPrintTemplateInvoice(GetSession(), id);
            } else {
                printTemplateInvoice = new PrintTemplateInvoice();
            }

            if (!IsPostBack) {
                tbTitle.Text = printTemplateInvoice.Title;
                tbAccepter.Text = printTemplateInvoice._accepter;
                tbBIK.Text = printTemplateInvoice._bik;
                tbBank.Text = printTemplateInvoice._bank;
                tbInn.Text = printTemplateInvoice._inn;
                tbKpp.Text = printTemplateInvoice._kpp;
                tbKsch.Text = printTemplateInvoice._ksch;
                tbLs.Text = printTemplateInvoice._ls;
                tbSp.Text = printTemplateInvoice._sp;
                tbSumm.Text = printTemplateInvoice._summ;
                tbVid.Text = printTemplateInvoice._vid;

            }
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            try {
                var ms = new MainService();

                printTemplateInvoice.Title = tbTitle.Text;
                printTemplateInvoice._accepter = tbAccepter.Text;
                printTemplateInvoice._bik = tbBIK.Text;
                printTemplateInvoice._bank = tbBank.Text;
                printTemplateInvoice._inn = tbInn.Text;
                printTemplateInvoice._kpp = tbKpp.Text;
                printTemplateInvoice._ksch = tbKsch.Text;
                printTemplateInvoice._ls = tbLs.Text;
                printTemplateInvoice._sp = tbSp.Text;
                printTemplateInvoice._summ = tbSumm.Text;
                printTemplateInvoice._vid = tbVid.Text;

                printTemplateInvoice = ms.SavePrintTemplateInvoice(GetSession(), printTemplateInvoice);
                if (comboActive.SelectedValue == "1") {
                    ms.SetInvoiceTemplateActive(GetSession(), printTemplateInvoice);
                }

                Session["success"] = "Изменения сохранены успешно";
            } catch (Exception ex) {

                if (ex.InnerException is SoapException
                    && ((SoapException)ex.InnerException).Detail.InnerText == "AuthorizationException") {
                    throw ex;
                } else {
                    Session["error"] = "Ошибка сохранения";
                }
            }
        }
    }
}